"""
Copyright 2009, Thomas Dejanovic, Jay Shurtz.
 
This is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.

This module exists to load hatch design files for hatching.
"""

id = "$Id: hatch_files.py 667 2010-07-01 00:12:17Z jayshurtz $"
# $URL: http://hatch.googlecode.com/svn/tags/taggle_release_2.2/hatch/hatch_library/hatch_files.py $
# $Author: jayshurtz $
version = " ".join(id.split()[1:3])


from hatch_constants import FILE_NAME, TOP_NODE
from hatch_iterators import HatchNodeIteratorBreadthFirst
import hatch_log

# Import everything needed for 'reading' a hatch file.
import to_hatch_structure
from hatch_node import HatchNode
from hatch_node_physical import HatchNodePhysical
from hatch_register import Register
from hatch_struct import Struct, Pack


def hatch_file(fileName, topNode=TOP_NODE):
    """ Get a hatch sub-design from another file to build multi-file designs.
        This functions automatically sets the FILE_NAME property of the sub-design top node to the passed-in file name.
        'fileName' is the hatch sub-design file, it is a string passed to 'execfile()'.
        'topNode' is the top node identifier in the sub-design, this is usually the hatch constant TOP_NODE.
        Returns the top hatch node in the sub-design.  This is usually added to the complete design
            with a statement like "x.childNodes.append(hatch_file('foo.py'))".
    """
    hatch_log.logger.debug(hatch_file.func_name + "(" + fileName + ", " + topNode + ")")
    fileLocals = {}
    execfile(fileName, globals(), fileLocals)   # Import cPack from it's own file, 'fileLocals' is used so that nothing in locals() is overwritten.

    try:
        topHatchNode = fileLocals[topNode]
    except KeyError, e:
        raise KeyError, repr(topNode) + " not found in " + repr(fileLocals) + " of " + repr(fileName) + "."
    topHatchNode.properties[FILE_NAME] = fileName   # Mark this node as imported from another file.
    return topHatchNode


def get_node_files(hatchNode):
    """ Get all the nodes in a design that have the FILE_NAME property set 
        (this usually includes the passed in top node.)
        This is used to hatch multiple target files from multi-file designs.
        'hatchNode' is the top node in the overall design.
        Returns a list of hatchNodes.
    """
    nodeList = []
    for node in HatchNodeIteratorBreadthFirst(hatchNode):
        if node.properties.get(FILE_NAME, None) is not None:
            nodeList.append(node)
    return nodeList


